#include "database_op.h"
void salt_generate(char *salt,int len_of_salt){//盐值生成
	for (int i = 0; i < len_of_salt; i++) {
		srand((unsigned)(i+time(NULL)));
		switch( rand() % 2 ){
			case 0:salt[i] = rand() % 26 + 'a';break;
			case 1:salt[i] = rand() % 26 + 'A';break;
		}
	}
}
void secret_key(char* salt,char* passwd,char* final_secret_key){//最终密文生成
	strcpy(final_secret_key,crypt(passwd,salt));
}
//注册到数据库
int register_to_database(char* UserName,char* Password){
	MYSQL *conn;
   // MYSQL_RES *res;
   // MYSQL_ROW row;
	const char* server="localhost";
	const char* user="root";
	const char* password="123";
	const char* database="NetDisk";
	//如果要传参数，使用sprintf使得参数与query拼接成一句完整命令
	char salt1[9]={0};
	salt_generate(salt1,8);
	char salt[20]={0};
	sprintf(salt,"$6$%s",salt1);
	char final_secret_key[200]={0};
	secret_key(salt,Password,final_secret_key);
	char query[200]={0};
	sprintf(query,"insert into UserPassword(UserName,salt,UserPasswd) values('%s','%s','%s');",UserName,salt,final_secret_key);
	int t;
	conn=mysql_init(NULL);
	if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
		printf("Error connecting to database:%s\n",mysql_error(conn));
	}else{
		printf("Connected...\n");
	}
	t=mysql_query(conn,query);//执行sql
	if(t){
		printf("Error making query:%s\n",mysql_error(conn));
	}else{
		printf("insert success,insert row=%ld\n",(long)mysql_affected_rows(conn));
	}
	bzero(query,200);
	sprintf(query,"insert into FileList(precode,filename,owner,filetype) values('%d','%s','%s','%s');",\
	0,UserName,UserName,"d");
	t=mysql_query(conn,query);//执行sql
	if(t){
		printf("Error making query:%s\n",mysql_error(conn));
	}else{
		printf("insert success,insert row=%ld\n",(long)mysql_affected_rows(conn));
	}
	mysql_close(conn);
	return 0;
}
//查找
int search_from_database(char* query,char* result){
	MYSQL *conn;//连接
	MYSQL_RES *res;//结果操作指针
	MYSQL_ROW row;//一行的数据是存在row中间
	const char* server="localhost";
	const char* user="root";
	const char* password="123";
	const char* database="NetDisk";//要访问的数据库名称
	unsigned int t;
	conn=mysql_init(NULL);//要初始化
	if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
		printf("Error connecting to database:%s\n",mysql_error(conn));
		return -1;
	}else{
		printf("Connected...\n");
	}
	t=mysql_query(conn,query);//执行sql语句
	if(t){
		printf("Error making query:%s\n",mysql_error(conn));//sql的语法出错
	}else{
		res=mysql_use_result(conn);//获取结果
		if(res){
			int len_of_info=0;
			//int i=0;
			while((row=mysql_fetch_row(res))!=NULL){
				for(t=0;t<mysql_num_fields(res);t++){//mysql_num_fields获取一行有几列
					strncpy(result+len_of_info,row[t],strlen(row[t]));
					len_of_info+=strlen(row[t]);
					strncpy(result+len_of_info," ",1);
					len_of_info++;
				}
			}
			printf("查找结果的长度=%ld,查找结果为:%s\n",strlen(result),result);
		}else{
			printf("Don't find data\n");
		}
		mysql_free_result(res);
	}
	mysql_close(conn);
	return 0;
}
//删除，插入或者修改
int delete_insert_updata(char* query){
	MYSQL *conn;
	const char* server="localhost";
	const char* user="root";
	const char* password="123";
	const char* database="test";
	int t;
	conn=mysql_init(NULL);
	if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
		printf("Error connecting to database:%s\n",mysql_error(conn));
		return -1;
	}else{
		printf("Connected...\n");
	}
	t=mysql_query(conn,query);
	if(t){
		printf("Error making query:%s\n",mysql_error(conn));//未能正确执行此语句
		return -1;
	}else{
		printf("operation success, affect_row=%ld\n",(long)mysql_affected_rows(conn));
	}
	mysql_close(conn);
	return 0;
}
// //修改
// int modify_(char* query){
// 	MYSQL *conn;
//    // MYSQL_RES *res;
//    // MYSQL_ROW row;
// 	const char* server="localhost";
// 	const char* user="root";
// 	const char* password="123";
// 	const char* database="NetDisk";
// 	int t;
// 	conn=mysql_init(NULL);
// 	if(!mysql_real_connect(conn,server,user,password,database,0,NULL,0)){
// 		printf("Error connecting to database:%s\n",mysql_error(conn));
// 		return -1;
// 	}else{
// 		printf("Connected...\n");
// 	}
// 	t=mysql_query(conn,query);
// 	if(t){
// 		printf("Error making query:%s\n",mysql_error(conn));//未能正确执行此语句
// 		return -1;
// 	}else{
// 		printf("change success,change row=%ld\n",(long)mysql_affected_rows(conn));
// 	}
// 	mysql_close(conn);
// 	return 0;
// }